package com.mapapp.flutter_mapapp.persenter;

import android.content.Context;

import com.mapapp.flutter_mapapp.application.Constants;
import com.mapapp.flutter_mapapp.bean.MapDetailBean;
import com.mapapp.flutter_mapapp.bean.MapResult;
import com.mapapp.flutter_mapapp.http.ActivityLifeCycleEvent;
import com.mapapp.flutter_mapapp.http.Api;
import com.mapapp.flutter_mapapp.http.HttpUtil;
import com.mapapp.flutter_mapapp.http.ProgressSubscriber;
import com.mapapp.flutter_mapapp.util.SharePreferenceUtil;
import com.mapapp.flutter_mapapp.util.ToastUtils;
import com.mapapp.flutter_mapapp.view.SearchViewInterface;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;

import rx.subjects.PublishSubject;

public class SearchPersenter {
    private Context mContext;
    private SearchViewInterface mView;
    private PublishSubject<ActivityLifeCycleEvent> lifecycleSubject;
    private String history = "";

    public SearchPersenter(Context context,SearchViewInterface view,PublishSubject<ActivityLifeCycleEvent> lifecycleSubject){
        this.mContext = context;
        this.mView = view;
        this.lifecycleSubject = lifecycleSubject;

        init();
    }

    private void init(){
        gethistory();
    }

    private void gethistory(){
        ArrayList<String> list = new ArrayList<>();
        history = (String)SharePreferenceUtil.get(mContext, Constants.SEARCH_HISTORY,"");
        if(!"".equals(history)){
            String[] historyList = history.split("#%!");
            LinkedHashSet<String> set = new LinkedHashSet<>();
            for(int i = 0 ; i < historyList.length ; i ++){
                set.add(historyList[i]);
            }
            Iterator<String> it = set.iterator();
            while(it.hasNext()) {
                list.add(it.next());
            }
            mView.initHistoryData(list);
        }
    }

    public void saveSearch(String search){
        if("".equals(history)){
            history = search;
        }else{
            history = search + "#%!" +history;
        }
        SharePreferenceUtil.put(mContext,Constants.SEARCH_HISTORY,history);
        gethistory();
    }

    public void deleteSearchHistory(){
        SharePreferenceUtil.put(mContext,Constants.SEARCH_HISTORY,"");
    }

    public void searchByaddress(String address){
        HttpUtil.getInstance().toSubscribe(Api.getApiService().searchByaddress(address),
                new ProgressSubscriber<List<MapDetailBean>>(mContext,false) {
                    @Override
                    protected void _onNext(List<MapDetailBean> data) {
                        mView.showSearchResult(data);
                    }

                    @Override
                    protected void _onError(String message) {
                        ToastUtils.showMessage(mContext, message);
                    }
                }, lifecycleSubject);
    }

    public void getAllAddress(){
        HttpUtil.getInstance().toSubscribe(Api.getApiService().community(),
                new ProgressSubscriber<List<MapDetailBean>>(mContext,false) {
                    @Override
                    protected void _onNext(List<MapDetailBean> data) {
                        mView.setAlladdress(data);
                    }

                    @Override
                    protected void _onError(String message) {
                    }
                }, lifecycleSubject);
    }
}
